package com.jmpt.fangbohui.http;

import java.util.Date;

import org.apache.http.cookie.Cookie;

import android.content.Context;

import com.jmpt.fangbohui.MyApplication;
import com.jmpt.fangbohui.utils.AppUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

/**
 * 
 * @author Victor
 *
 */
public class AsyncHttp {
    public static AsyncHttpClient client;

    public static AsyncHttpClient getClient() {
        if (client == null) {
            client = new AsyncHttpClient();
            client.addHeader("os", "Android");
            client.addHeader("version", String.valueOf(AppUtils.getVersion(MyApplication.getSingleInstance())));
            client.setTimeout(20000);
            PersistentCookieStore myCookieStore = new PersistentCookieStore(MyApplication.getSingleInstance());
            client.setCookieStore(myCookieStore);
        }
        return client;
    }

    public static RequestHandle get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {


        return getClient().get(url, params, responseHandler);
    }

    public static RequestHandle post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {

        return getClient().post(url, params, responseHandler);

    }

    public static RequestHandle get(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {

        return getClient().get(context, url, params, responseHandler);
    }

    public static RequestHandle post(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {

        return getClient().post(context, url, params, responseHandler);

    }

}
